/**
 * 🐖场景
 */
class ScreenGame extends egret.DisplayObjectContainer {
    static Main: egret.DisplayObjectContainer
    public constructor() {
        super()
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
    }
    private init() {
        const miva = this
        miva.sortableChildren = true
        ScreenGame.Main = miva.createContainerMain()
        miva.createController()
        miva.createMaskTip()
    }
    // 创建提示遮罩
    private createMaskTip() {
        const miva = this
        const _container = new egret.Sprite()
        const png = Utils.createBitmapByName('mask_tip_png')
        const btn = Utils.createBitmapByName('btn_tip_mask_png')
        _container.addChild(png)
        _container.addChild(btn)
        miva.addChild(_container)

        png.anchorOffsetY = png.height
        png.y = miva.stage.stageHeight
        png.touchEnabled = true
        png.addEventListener(egret.TouchEvent.TOUCH_TAP, Utils.superStopPropagation, miva)
        Utils.anchorCenter(btn)
        btn.x = miva.stage.stageWidth * .5
        btn.y = miva.stage.stageHeight * .5
        btn.touchEnabled = true
        btn.addEventListener(egret.TouchEvent.TOUCH_TAP, evt => {
            Utils.superStopPropagation(evt)
            miva.removeChild(_container)
        }, miva)
    }
    // 创建主场景容器
    private createContainerMain() {
        const miva = this
        const _container = new egret.Sprite()
        _container.sortableChildren = true
        miva.createBg(_container)
        miva.addChild(_container)
        miva.createContainerRole(_container)
        _container.y = -55

        return _container
    }
    // 创建主场景背景
    private createBg(container: egret.DisplayObjectContainer) {
        const miva = this
        const png = Utils.createBitmapByName(`bg_chun_png`)
        container.addChild(png)
        // 监听切换背景事件
        miva.stage.addEventListener("SWITCH_BG", evt => {
            const {data} = evt
            png.texture = RES.getRes(`${data}_png`)
        }, miva)
    }
    // 创建角色容器
    private createContainerRole(container: egret.DisplayObjectContainer) {
        const miva = this
        const _container = new egret.Sprite()
        const {height} = container
        miva.createRole(_container)
        miva.createRoleYF(_container)
        container.addChild(_container)
        _container.scaleY = _container.scaleX = .5
        _container.anchorOffsetX = _container.width * .5
        _container.anchorOffsetY = _container.height
        _container.x = miva.stage.stageWidth * .5
        _container.y = height
        _container.zIndex = 998
    }
    // 创建角色
    private createRole(container: egret.DisplayObjectContainer) {
        const miva = this
        const role = Utils.createBitmapByName(`role_girl_png`)
        let sex = !0
        container.addChild(role)
        // 监听切换性别事件
        miva.addEventListener("SWITCH_SEX", _ => {
            sex = !sex
            role.texture = sex ? RES.getRes('role_girl_png') : RES.getRes('role_boy_png')
            ParamsPool.SEX === 'boy'
                ? ParamsPool.SEX = 'girl'
                : ParamsPool.SEX = 'boy'
        }, miva)
    }
    // 创建角色装扮
    private createRoleYF(container: egret.DisplayObjectContainer) {
        const miva = this
        const cloth = {
            wz: new egret.Bitmap(),
            xz: new egret.Bitmap(),
            rj: new egret.Bitmap(),
            yf: new egret.Bitmap(),
            bb: new egret.Bitmap()
        }

        for (let temp in cloth) {
            container.addChild(cloth[temp])
        }

        miva.addEventListener('SWITCH_SEX', _ => {
            for (let temp in cloth) {
                cloth[temp].texture = null
            }
        }, miva)

        miva.stage.addEventListener('SWITCH_YF', evt => {
            const {data: {item, type}} = evt
            const png = cloth[type]
            png.texture = RES.getRes(`${item}_png`)
        }, miva)
    }
    // 创建控制器
    private createController() {
        const miva = this
        const controller = new Controller()

        miva.addChild(controller)

        controller.anchorOffsetY = controller.height
        controller.y = miva.stage.stageHeight

        miva.stage.addEventListener('CREATE_ITEM', _ => {
            console.log(123)
            controller.zIndex = 999
        }, miva)

        miva.stage.addEventListener('FINISH', _ => {
            miva.removeChild(controller)
            const copy = Utils.createBitmapByName('bottom_bg_jpg')
            miva.addChild(copy)
            copy.anchorOffsetY = copy.height
            copy.y = miva.stage.stageHeight

            egret.Tween.get(miva).wait(200).call(_ => {
                miva.createGroupBtnFinish()
            })
        }, miva)
    }
    // 创建截图后的按钮组
    private createGroupBtnFinish() {
        const miva = this
        const container = new egret.Sprite()
        // const btnFenXiang = Utils.createBitmapByName(`btn_fenxiang_png`)
        // const btnRePlay = Utils.createBitmapByName(`btn_replay_png`)
        // const tip = Utils.createBitmapByName(`tip_save_png`)
        const divImage = document.getElementById("divImage");//获取DIV
        const btnFenXiang = document.getElementById("miva-fenxiang");
        const btnRePlay = document.getElementById("miva-replay");

        // container.addChild(btnFenXiang)
        // container.addChild(btnRePlay)
        // container.addChild(tip)
        // miva.addChild(container)

        // Utils.dragDisplayObject({
        //     target:container,
        //     container:miva,
        //     that:miva
        // })

        // tip.x = 175
        // tip.y = 40
        
        // btnFenXiang.y = btnRePlay.height + 30

        // container.x = 60
        // container.y = 965



        // // 分享按钮点击事件
        // btnFenXiang.touchEnabled = true
        btnFenXiang.addEventListener('click', _ => {
            const mask = Utils.createBitmapByName(`mask_fenxiang_png`)
            divImage.style.display = "none";//显示DIV
            miva.stage.addChild(mask)
            mask.touchEnabled = true
            mask.addEventListener(egret.TouchEvent.TOUCH_TAP, _ => {
                divImage.style.display = "block";//显示DIV
                miva.stage.removeChild(mask)
            }, miva)
        })

        // 重玩按钮点击事件
        // btnRePlay.touchEnabled = true
        btnRePlay.addEventListener('click', _ => {
            window.location.href = window.location.href + "?" + Math.random();
        })
    }
}